package com.javablog.redis.demo.lock;

import java.io.Closeable;

/**
 * @author ycd0075
 * @date 2023-04-07 13:51
 */
public class Lock implements Closeable {
    private LockRepository repository;
    private String key;
    private String value;
    //500 ms
    private static final long REDIS_TTL = 500;

    public Lock(LockRepository repository, String key, String value) {
        this.repository = repository;
        this.key = key;
        this.value = value;
    }

    public boolean lock() {
        return repository.tryLock(key, value, REDIS_TTL);
    }

    @Override
    public void close() {
        release();
    }

    public void release() {
        repository.releaseLock(key, value);
    }
}